package com.zz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.common.R;
import com.zz.entity.SystemNotice;
import com.zz.entity.User;
import com.zz.mapper.SystemNoticeMapper;
import com.zz.mapper.UserMapper;
import com.zz.service.SystemNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统通知信息
 * @author HUAWEI
 */
@Service
public class SystemNoticeServiceImpl extends ServiceImpl<SystemNoticeMapper, SystemNotice> implements SystemNoticeService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SystemNoticeMapper systemNoticeMapper;

    @Override
    public R<List<SystemNotice>> show(HttpServletRequest request) {
        // 从session中获取当前用户名
        String userName = (String) request.getSession().getAttribute("user");

        // 条件查询器
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 通知信息：条件查询器
        LambdaQueryWrapper<SystemNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemNotice::getUserId, user.getId());
        List<SystemNotice> systemNotices = systemNoticeMapper.selectList(wrapper);

        if(systemNotices.isEmpty()){
            return R.error("没有可读信息！");
        }

        return R.success(systemNotices);
    }

    @Override
    public R<List<SystemNotice>> showUnRead(HttpServletRequest request) {
        // 从session中获取当前用户名
        String userName = (String) request.getSession().getAttribute("user");

        // 条件查询器
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 通知信息：条件查询器
        LambdaQueryWrapper<SystemNotice> wrapper = new LambdaQueryWrapper<>();
        // 未读
        wrapper.eq(SystemNotice::getUserId, user.getId()).eq(SystemNotice::getIsRead, 0);
        List<SystemNotice> systemNotices = systemNoticeMapper.selectList(wrapper);

        if (systemNotices.isEmpty()){
            return R.error("没有未读信息！");
        }

        return R.success(systemNotices);
    }

    @Override
    public R<List<SystemNotice>> showRead(HttpServletRequest request) {
        // 从session中获取当前用户名
        String userName = (String) request.getSession().getAttribute("user");

        // 条件查询器
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 通知信息：条件查询器
        LambdaQueryWrapper<SystemNotice> wrapper = new LambdaQueryWrapper<>();
        // 已读
        wrapper.eq(SystemNotice::getUserId, user.getId()).eq(SystemNotice::getIsRead, 1);
        List<SystemNotice> systemNotices = systemNoticeMapper.selectList(wrapper);

        if (systemNotices.isEmpty()){
            return R.error("没有已读信息!");
        }

        return R.success(systemNotices);
    }

    @Override
    public R<String> deleteRead(int id) {
        // 从数据库中查询通知信息
        SystemNotice systemNotice = systemNoticeMapper.selectById(id);

        if (systemNotice.getIsRead() == 0){
            return R.error("此消息未读!");
        }

        // 从数据库删除此条消息
        systemNoticeMapper.deleteById(id);

        return R.success("成功删除消息通知！");
    }

    @Override
    public R<String> letRead(int id) {
        // 从数据库中查询通知信息
        SystemNotice systemNotice = systemNoticeMapper.selectById(id);

        if (systemNotice.getIsRead() == 1){
            return R.error("此通知已读！");
        }

        // 将消息设置为已读
        systemNotice.setIsRead(1);

        // 同步更新到数据库
        systemNoticeMapper.updateById(systemNotice);

        return R.success("成功将通知设置为已读！");
    }
}
